<template>
  <el-dialog class="app-container" :title="title"  width="800px"  :visible.sync="visible" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" prop="warehouseNo">
        <el-input
          v-model="queryParams.warehouseNo"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="warehouseList" ref="tb" @selection-change="handleSelectionChange"  >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="仓库编号" align="center" prop="warehouseNo" />
      <el-table-column label="所属地区" align="center" prop="region" />
      <el-table-column label="客户地址" align="center" prop="address" />
      <el-table-column label="负责部门" align="center" prop="responsibleDeptName" />
      <el-table-column label="负责人" align="center" prop="responsibleUserName" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="状态" align="center" prop="status" />-->

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div  slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleImportWarehouse" >确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse } from "@/api/module/warehouse";
import {provinceNameList} from "@/api/module/region";

export default {
  name: "Warehouse",
  data() {
    return {
      // 遮罩层
      loading: true,
      visible :false,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      warehouseList: [],
      // 弹出层标题
      title: "选择仓库",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: null,
        warehouseNo: null,
        regionId: null,
        region: null,
        address: null,
        responsibleDept: null,
        responsibleDeptName: null,
        responsibleUser: null,
        responsibleUserName: null,
        createId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      regionOptions: [], //地区选择列表
      addressOptions: [], //地址选择列表
      optionProps: {
        value: 'label',
        label: 'label',
        children: 'children'
      },
      isMultiple:0,
      index:null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 显示弹框
    show(index,isMultiple) {
      debugger
      this.getList();
      this.visible = true;
      if(isMultiple){
        this.isMultiple=isMultiple;
      }
      if(typeof index === 'number' ){
        this.index=index;
      }
    },
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseName: null,
        warehouseNo: null,
        regionId: null,
        region: null,
        address: null,
        responsibleDept: null,
        responsibleDeptName: null,
        responsibleUser: null,
        responsibleUserName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        createId: null,
        remark: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.warehouseNo=this.getWarehouseNo();
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWarehouse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除仓库编号为"' + ids + '"的数据项？').then(function() {
        return delWarehouse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    },

    // 地址选择
    addressChange(value) {
      this.form.address = value[0]+value[1]+value[2];
    },
    // 地址选择
    regionChange(value) {

      this.form.region=value.label;
      this.form.regionId=value.value;
    },
    // 选中数据
    handleSelectionChange(selection) {
      if(selection.length>0){
        if(this.isMultiple && this.isMultiple ==1){
          this.ids = selection.map(item => item.id)
          this.names = selection.map(item => item.warehouseName)

        }else{
          //单选
          this.ids=selection[0].id;
          this.names=selection[0].warehouseName;
          if (selection.length > 1) {
            this.$refs.tb.clearSelection();
            this.$refs.tb.toggleRowSelection(selection.pop());
          }
        }
      }
    },
    currentChange(currentRow) {
      this.$refs.tb.toggleRowSelection(currentRow)
    },
    handleImportWarehouse() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$modal.msgError("请选择仓库");
        return;
      }
      this.visible = false;
      let data = {
        id:this.ids,
        name:this.names,
        index:this.index
      }
      if(this.isMultiple && this.isMultiple ==1){
        data = {
          id:this.ids.join(","),
          name:this.names.join(",")
        }
      }

      this.$emit("showWarehouse",data);
    }
  }
};
</script>
